﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace ScorchedTanks
{
    class Explosion : Actor3d
    {
        public bool isInUse = false;
        float timetolive = 0;
        public float size;

        public Explosion(ContentManager content, String model)
        {
            mModel = content.Load<Model>(model);
        }

        public void Explode(Vector3 p ){
            isInUse = true;
            position = p;
            timetolive = 0;
        }

        public override void update(float dTime)
        {
            if (isInUse)
            {
                   
                timetolive += dTime;
                if (timetolive <= 600)
                {
                    size += 1.5f;
                 }
                else
                {
                    timetolive = 0;
                    readyToexit = true;
                    isInUse = false;
                    size = 1.0f;

                }
            }
            base.update(dTime);
        }

        public override void Draw(GraphicsDeviceManager g, Camera cam)
        {
 
            Matrix[] transforms = new Matrix[mModel.Bones.Count];
            mModel.CopyAbsoluteBoneTransformsTo(transforms);

            Matrix worldMatrix = Matrix.CreateScale(size) * Matrix.CreateTranslation(position);

            g.GraphicsDevice.RenderState.AlphaBlendOperation = BlendFunction.Add;
            g.GraphicsDevice.RenderState.AlphaSourceBlend = Blend.SourceAlpha;
            g.GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
            g.GraphicsDevice.RenderState.AlphaBlendEnable = true;   
     
            foreach (ModelMesh mesh in mModel.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World = transforms[mesh.ParentBone.Index] * worldMatrix;
                    effect.View = Matrix.CreateLookAt(cam.GetPosition(), cam.GetLookAt(), Vector3.Up);
                    effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 4 / 3, 1, 10000);

                    effect.EnableDefaultLighting();
                    effect.PreferPerPixelLighting = true;
                }
                mesh.Draw();
            }
            g.GraphicsDevice.RenderState.DepthBufferWriteEnable = true;
            g.GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
            g.GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
        }

    }
}
